package com.have.blog.web.controller;

import com.have.blog.base.wrapper.CommonPage;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.web.dto.BlogCategoryDto;
import com.have.blog.web.dto.BlogDto;
import com.have.blog.web.service.BlogCategoryService;
import com.have.blog.web.service.WebBlogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/classify")
@Api(value = "分类相关接口", tags = {"分类相关接口"})
@Slf4j
public class BlogCategoryController {

    @Autowired
    private BlogCategoryService blogCategoryService;
    @Autowired
    private WebBlogService blogService;

    /**
     * 获取分类的信息
     */
    @ApiOperation(value = "获取分类的信息", notes = "获取分类的信息")
    @GetMapping("/getBlogSortList")
    public Wrapper getBlogSortList() {
        log.info("获取分类信息");
        List<BlogCategoryDto> resultList = blogCategoryService.getList();
        return Wrapper.success(resultList);
    }

    @ApiOperation(value = "通过blogSortUid获取文章", notes = "通过blogSortUid获取文章")
    @GetMapping("/getArticleByBlogSortUid")
    public Wrapper<CommonPage> getArticleByBlogSortUid(HttpServletRequest request,
                                                       @ApiParam(name = "blogSortUid", value = "分类UID", required = false) @RequestParam(name = "blogSortUid", required = false) Integer blogSortUid,
                                                       @ApiParam(name = "pageNum", value = "当前页数", required = false) @RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                       @ApiParam(name = "pageSize", value = "每页显示数目", required = false) @RequestParam(name = "pageSize", required = false, defaultValue = "10") Integer pageSize) {

        if (blogSortUid == null) {
            log.info("点击分类,传入BlogSortUid不能为空");
            return Wrapper.failed();
        }
        log.info("通过blogSortUid获取文章列表");
        List<BlogDto> resultList = blogService.getListByBlogSortUid(blogSortUid, pageNum, pageSize);
        return Wrapper.success(CommonPage.restPage(resultList));
    }
}
